package com.lagou.mapper;

import com.lagou.pojo.User;
import org.apache.ibatis.annotations.*;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

@CacheNamespace(implementation = RedisCache.class)
public interface UserMapper {

    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.OrderMapper.findOrderByUid")),
    })
    List<User> findAll();

    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.RoleMapper.findRoleByUid")),
    })
    List<User> findAllUserAndRole();

    @Insert("insert into user (id, username) values(#{id}, #{username})")
    void addUser(User user);

    @Update("update user set username = #{username} where id = #{id}")
    void updateUser(User user);

    @Select("select * from user")
    List<User> selectUser();

    @Delete("delete from user where id = #{id}")
    void deleteUser(int id);

    @Select("select * from user where id = #{id}")
    User findUserById(Integer id);
}
